package panel;

import builder.JadBuilder;
import builder.JarBuilder;
import builder.DatBuilder;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author daz
 */
public class BuildFileScreen extends JPanel implements Runnable {

    private int cavanWidth;
    private int canvasHeight;
    private boolean isRunning = false;
//    private Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 18);
    private JTextField pathField;
    private JButton openFileButton;
    private ActionListener openFileListenner;
    private JButton buildButton;
    private ActionListener buildListenner;
    private JFileChooser fileChooser;
    private JLabel syntaxlLabel;
    private JTextField syntaxlInputField;
    private JLabel cpNameLabel;
    private JTextField cpInputField;
    private JLabel cpIdLabel;
    private JTextField cpIdInputField;
    private JLabel numberActivateLabel;
    private JTextField numberActivateInputField;
    private JLabel chargeMoney1Label;
    private JTextField chargeMoney1InputField;
    private JLabel chargeMoney2Label;
    private JTextField chargeMoney2InputField;
    private JLabel chargeMoney3Label;
    private JTextField chargeMoney3InputField;
    private JLabel chargeMoneyRatioLabel;
    private JTextField chargeMoneyRatioInputField;
    private JLabel databaseIpLabel;
    private JTextField databaseIpInputField;
    private JLabel databaseUserLabel;
    private JTextField databaseUserInputField;
    private JLabel databasePassLabel;
    private JTextField databasePassInputField;
    private JLabel newFileNameLabel;
    private JTextField newFileNameInputField;
//    private JFrame frame;
    private DatBuilder datBuilder;
    private JarBuilder jarBuilder;
    private JadBuilder jadBuilder;
    private JButton deleteCpButton;
    private JButton addCpButton;
    private JButton editCpButton;
    private ActionListener addCpListenner;
    private ActionListener editCpListenner;
    private ActionListener deleteCpListenner;
    private final JFrame frame;

    public BuildFileScreen(JFrame frame) {
        this.frame = frame;
        initValue();
        initBuilder();
        initListenner();
        initComponents();
    }

    private void initBuilder() {
        datBuilder = new DatBuilder();
        jarBuilder = new JarBuilder();
        jadBuilder = new JadBuilder();
    }

    private void initListenner() {
        openFileListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fileChooser.showOpenDialog(BuildFileScreen.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fileChooser.getSelectedFile();
                    try {
                        String foldePath = file.getParent();
                        String fileName = file.getName();
                        datBuilder.setFolderPath(foldePath);
                        jarBuilder.setPathFolder(foldePath);
                        jarBuilder.setJarName(fileName);
                        jadBuilder.setFolderPath(foldePath);
                        System.out.println("test :" + file.getParent());
                    } catch (Exception ex) {
                        Logger.getLogger(BuildFileScreen.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    pathField.setText(file.getAbsolutePath());
                } else {
                }
            }
        };
        buildListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                jarBuilder.setNewJarName(newFileNameInputField.getText());
                jadBuilder.setNewFileName(newFileNameInputField.getText());
                datBuilder.creatDatFile(cpInputField.getText(), cpIdInputField.getText());
                jarBuilder.creatFile();
                jadBuilder.creatDatFile();
                JOptionPane.showMessageDialog(frame,
                        "Tạo file thành công.");
            }
        };

    }

    public void start() {
        isRunning = true;
        Thread thread = new Thread(this);
        thread.start();
    }

    private void initComponents() {
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 100;
        int linePadding = 30;
        fileChooser = new JFileChooser();

        pathField = new JTextField("");
//        pathField.setFont(font);
        add(pathField);
        pathField.setBounds(left + 30, top, 250, height);
        //open file
        openFileButton = new JButton("Chọn file");
        add(openFileButton);
        openFileButton.addActionListener(openFileListenner);
        openFileButton.setBounds(cavanWidth - 100, top, width - 20, height);
        //cp line
        top += linePadding;
        cpNameLabel = new JLabel("Tên CP :");
        cpNameLabel.setBounds(left + 30, top, 250, height);
        add(cpNameLabel);
        cpInputField = new JTextField();
        cpInputField.setBounds(left + 130, top, 150, height);
        add(cpInputField);
        cpIdLabel = new JLabel("Id :");
        cpIdLabel.setBounds(left + 300, top, 30, height);
        add(cpIdLabel);
        cpIdInputField = new JTextField();
        cpIdInputField.setBounds(left + 330, top, 50, height);
        add(cpIdInputField);
        // sysntax line
        top += linePadding;
        syntaxlLabel = new JLabel("Cú pháp tin nhắn :");
        syntaxlLabel.setBounds(left + 30, top, 250, height);
        add(syntaxlLabel);
        syntaxlInputField = new JTextField();
        syntaxlInputField.setBounds(left + 130, top, 150, height);
        add(syntaxlInputField);
        // activate line
        top += linePadding;
        numberActivateLabel = new JLabel("Đầu số kích hoạt :");
        numberActivateLabel.setBounds(left + 30, top, 250, height);
        add(numberActivateLabel);
        numberActivateInputField = new JTextField();
        numberActivateInputField.setBounds(left + 130, top, 150, height);
        add(numberActivateInputField);
        // charge money 1 line
        top += linePadding;
        chargeMoney1Label = new JLabel("Đầu số nạp xu  :");
        chargeMoney1Label.setBounds(left + 30, top, 250, height);
        add(chargeMoney1Label);
        chargeMoney1InputField = new JTextField();
        chargeMoney1InputField.setBounds(left + 130, top, 150, height);
        add(chargeMoney1InputField);
        // charge money 2 line
        top += linePadding;
        chargeMoney2Label = new JLabel("Đầu số nạp xu  :");
        chargeMoney2Label.setBounds(left + 30, top, 250, height);
        add(chargeMoney2Label);
        chargeMoney2InputField = new JTextField();
        chargeMoney2InputField.setBounds(left + 130, top, 150, height);
        add(chargeMoney2InputField);
        // charge money 3 line
        top += linePadding;
        chargeMoney3Label = new JLabel("Đầu số nạp xu  :");
        chargeMoney3Label.setBounds(left + 30, top, 250, height);
        add(chargeMoney3Label);
        chargeMoney3InputField = new JTextField();
        chargeMoney3InputField.setBounds(left + 130, top, 150, height);
        add(chargeMoney3InputField);
        // charge money ratio  line
        top += linePadding;
        chargeMoneyRatioLabel = new JLabel("Hệ số nạp xu  :");
        chargeMoneyRatioLabel.setBounds(left + 30, top, 250, height);
        add(chargeMoneyRatioLabel);
        chargeMoneyRatioInputField = new JTextField();
        chargeMoneyRatioInputField.setBounds(left + 130, top, 150, height);
        add(chargeMoneyRatioInputField);
        // database adress  line
        top += linePadding;
        databaseIpLabel = new JLabel("Địa chỉ database :");
        databaseIpLabel.setBounds(left + 30, top, 250, height);
        add(databaseIpLabel);

        databaseIpInputField = new JTextField();
        databaseIpInputField.setBounds(left + 130, top, 150, height);
        add(databaseIpInputField);
        // database user line
        top += linePadding;
        databaseUserLabel = new JLabel("Database User:");
        databaseUserLabel.setBounds(left + 30, top, 250, height);
        add(databaseUserLabel);

        databaseUserInputField = new JTextField();
        databaseUserInputField.setBounds(left + 130, top, 150, height);
        add(databaseUserInputField);
        // database pass line
        top += linePadding;
        databasePassLabel = new JLabel("Database Password:");
        databasePassLabel.setBounds(left + 30, top, 250, height);
        add(databasePassLabel);

        databasePassInputField = new JTextField();
        databasePassInputField.setBounds(left + 130, top, 150, height);
        add(databasePassInputField);

        // new file name line
        top += linePadding;
        newFileNameLabel = new JLabel("Tên file mới:");
        newFileNameLabel.setBounds(left + 30, top, 250, height);
        add(newFileNameLabel);

        newFileNameInputField = new JTextField();
        newFileNameInputField.setText("newFile");
        newFileNameInputField.setBounds(left + 130, top, 150, height);
        add(newFileNameInputField);
        // build
        top += linePadding;
        buildButton = new JButton("Build file");
        add(buildButton);
        buildButton.addActionListener(buildListenner);
        buildButton.setBounds(cavanWidth - 100, top, width - 20, height);
        // add cp button
        addCpButton = new JButton("Thêm cp");
        add(addCpButton);
        addCpButton.addActionListener(addCpListenner);
        addCpButton.setBounds(left + 30, top, width - 20, height);
        // edit cp button
        left += 10 + width;
        editCpButton = new JButton("Sửa cp");
        add(editCpButton);
        editCpButton.addActionListener(editCpListenner);
        editCpButton.setBounds(left + 10, top, width - 20, height);
        // delete cp button
        left += width;
        deleteCpButton = new JButton("Xóa cp");
        add(deleteCpButton);
        deleteCpButton.addActionListener(deleteCpListenner);
        deleteCpButton.setBounds(left, top, width - 30, height);



    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(cavanWidth, canvasHeight);
    }

    private void initValue() {
        this.cavanWidth = 400;
        this.canvasHeight = 420;
    }

    @Override
    protected void paintComponent(Graphics g) {
//        g.setColor(hex2Rgb("#ffffff"));
//        g.fillRect(0, 0, cavanWidth, canvasHeight);
    }

    /**
     * 
     * @param colorStr e.g. "#FFFFFF"
     * @return 
     */
    public static Color hex2Rgb(String colorStr) {
        return new Color(
                Integer.valueOf(colorStr.substring(1, 3), 16),
                Integer.valueOf(colorStr.substring(3, 5), 16),
                Integer.valueOf(colorStr.substring(5, 7), 16));
    }

    @Override
    public void run() {
        while (isRunning) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException ex) {
            }
        }
    }
}
